<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		/*构造方法中加载模型,并重命名为article*/
		$this->load->model('article_model','article');
	}

	/*将客户端表单验证规则放入全局数组内*/
	private $formconfig = array(
		array(
			'field' => 'title',
			'label' => '标题',
			'rules' => 'trim|required',
		),
		array(
			'field' => 'content',
			'label' => '内容',
			'rules' => 'required',
		)
   	);
	 
	/*显示全部记录*/
	public function index(){
		/*从模型中调用方法获取全部记录列表*/
		$data['article']=$this->article->article_list();
		/*分页类的变量默认为空*/
		$data['links'] = '';
		/*加载视图,分配变量*/
		$this->load->view('article.html',$data);
	}

	/*分页显示记录*/
	public function articleList(){
		/*后台设置后缀为空，否则分页出错*/
		$this->config->set_item('url_suffix', '');
		/*载入分页类*/
		$this->load->library('pagination');
		/*每页显示数*/
		$perPage = 5;

		/*配置分页类*/
		/*指向你的分页所在的控制器类/方法的完整的URL*/
		$config['base_url'] = site_url('article/articleList');
		/*需要做分页的数据的总行数*/
		$config['total_rows'] = $this->db->count_all_results('article');
		/*每个页面中希望展示的数量*/
		$config['per_page'] = $perPage;
		/*给路径添加一个自定义后缀，后缀位于偏移段的后面*/
		$config['suffix'] = '.html';
		/*分页方法自动检测你 URI 的哪一段包含页数*/
		$config['uri_segment'] = 3;
		/*放在你当前页码的前面和后面的“数字”链接的数量*/
		$config['num_links'] = 2;
		/*默认分页的 URL 中显示的是你当前正在从哪条记录开始分页，如果你希望显示实际的页数，将该参数设置为 TRUE*/
		$config['use_page_numbers'] = TRUE;
		/*起始标签放在所有结果的左侧*/
		$config['full_tag_open'] = '<p class="p-page">';
		/*结束标签放在所有结果的右侧*/
		$config['full_tag_close'] = '</p>';
		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';

		/*初始化分页类,传递配置参数*/
		$this->pagination->initialize($config);

		/*创建分页按钮*/
		$data['links'] = $this->pagination->create_links();

		/*根据传递的uri参数获取偏移量*/
		$offset = $this->uri->segment(3);
		
		/*从数据库里取出指定书目的记录*/
		$data['article']=$this->article->article_list_limit($perPage, $offset);
		
		/*加载视图,分配变量*/
		$this->load->view('article.html',$data);
	}

	/*单条详细记录内容*/
	public function detail(){
		/*获取uri的第三个参数*/
		$aid = $this->uri->segment(3);
		/*根据参数调用模型中的方法查询,返回查询对象*/
		$data['article']=$this->article->article_one($aid);
		/*加载视图,分配变量*/
		$this->load->view('article_details.html',$data);
	}
	
	/*显示视图,客户端数据通过ajax动态请求*/
	public function jsonList(){
		$this->load->view('article_jsonlist.html');
	}

	/*响应客户端发出的ajax请求*/
	public function json(){
		/*从数据库中取出所有文章*/
		$data['article']=$this->article->article_all();
		/*将数组以json格式返回给客户端*/
		echo json_encode($data);
	}

	/*添加文章*/
	public function add(){

		/*设置表单验证的规则*/
		$this->form_validation->set_rules($this->formconfig);

		/*验证失败,重新显示表单*/
		if ($this->form_validation->run() == FALSE){
			$this->load->view('add_article.html');
		}
		else{
			/*验证成功*/
			/*配置文件上传类*/
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png|jpeg';
			$config['max_size'] = '10000';
			$config['file_name'] = time() . mt_rand(1000,9999);

			/*加载文件上传类,传递配置参数*/
			$this->load->library('upload', $config);
			/*执行文件上传*/
			$status = $this->upload->do_upload('thumb');
			/*返回错误信息*/
			$wrong = $this->upload->display_errors();

			/*如果出错,显示错误信息*/
			if($wrong){
				error($wrong);
			}
			/*返回数据信息*/
			$info = $this->upload->data();
			
			/*配置缩略图*/
			$arr['image_library'] = 'gd2';
			/*指定处理后图片的存放路径*/
			$arr['source_image'] = $info['full_path'];
			/*设置为true，新生成一个缩微图，false直接替换原图*/
			$arr['create_thumb'] = FALSE;
			/*保持原图宽高比*/
			$arr['maintain_ratio'] = TRUE;
			/*设置处理后图片放置路径*/
			$arr['new_image'] =  './uploads/thumb/'.$info['file_name'];
			$arr['width'] = 200;
			$arr['height'] = 200;

			/*载入缩略图类*/
			$this->load->library('image_lib', $arr);
			/*执行动作*/
			$status = $this->image_lib->resize();
			/*如果出错,显示错误信息*/
			if(!$status){
				error('缩略图动作失败');
			}
			/*如果成功,将相关信息保存到数组中*/
			$data = array(
				'title'=>$this->input->post('title'),
				'content'=>$this->input->post('content'),
				'thumb'=> $info['file_name'],
				'time'=>time()
			);
			/*调用模型中的方法,将记录插入到数据库*/
			$back = $this->article->insert($data);
			/*输出成功信息*/
			success('article', '发表成功');
		}

	}

	/*编辑文章*/
	public function edit(){
		/*获取uri的第三段参数*/
		$aid = $this->uri->segment(3);
		/*调用模型中的方法,获取一条记录的具体内容*/
		$data['article']=$this->article->article_one($aid);

		/*设置客户端表单验证规则*/
		$this->form_validation->set_rules($this->formconfig);

		/*表单验证失败,重新加载视图*/
		if ($this->form_validation->run() == FALSE){
			$this->load->view('edit_article.html',$data);
		}
		else{
			/*验证成功,将表单的信息放入数组*/
			$aid = $this->input->post('aid');
			$data = array(
				'aid'=> $aid,
				'title'=>$this->input->post('title'),
				'content'=>$this->input->post('content')
			);
			/*调用模型中的方法更新数据*/
			$back = $this->article->update($aid,$data);
			/*显示成功信息*/
			if($back){
				success('article', '修改成功！');
			}
		}
	}

	/*删除文章*/
	public function delete(){
		/*获取uri的第三段参数*/
		$aid = $this->uri->segment(3);
		/*根据第三段参数调用模型中的方法删除记录*/
		$state = $this->article->delete($aid);
		/*将数组类型数据转换为json类型数据返回*/
		echo json_encode(array('state'=>$state));
	}
	public function addDo(){//单独执行新增提交
		$data = array(
			'title'=>$this->input->post('title'),
			'content'=>$this->input->post('content')
		);
		$back = $this->article->insert($data);
		echo $back;
	}
	
	public function editDo(){//单独执行编辑提交
		$aid = $this->input->post('aid');
		$data = array(
			'aid'=> $aid,
			'title'=>$this->input->post('title'),
			'content'=>$this->input->post('content')
		);
		$back = $this->article->update($aid,$data);
		echo $back;
	}
}
